package lims.client;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.fit.cssbox.css.CSSNorm;
import org.fit.cssbox.css.DOMAnalyzer;
import org.fit.cssbox.io.DOMSource;
import org.fit.cssbox.io.DefaultDOMSource;
import org.fit.cssbox.io.DocumentSource;
import org.fit.cssbox.layout.BrowserCanvas;
import org.fit.cssbox.layout.Viewport;
import org.fit.cssbox.render.BoxRenderer;
import org.fit.cssbox.render.GraphicsRenderer;
import org.fit.net.DataURLHandler;
import org.w3c.dom.Document;


public class LimsPage implements Printable{
	private static Log logger=LogFactory.getLog(LimsPage.class);
	public Double scale;
	public int pageCount=0;
	private BrowserCanvas canvas;
	public LimsPage(BrowserCanvas canvas){
		this.canvas=canvas;
	}
	
	@Override
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
			throws PrinterException {
		logger.debug(String.format("printing page:%d",pageIndex));
		if(pageIndex>=20) {
			logger.debug(String.format("page index exceeds 20"));
			return NO_SUCH_PAGE;
		}
		Graphics2D g=null;
		try {
			int docWidth,docHeight;
			docWidth=(int) pageFormat.getImageableWidth();
			docHeight=(int) pageFormat.getImageableHeight();	
			g=(Graphics2D) graphics.create((int)pageFormat.getImageableX(),(int)pageFormat.getImageableY(),docWidth,docHeight);
			
			if(pageFormat.getOrientation()==PageFormat.REVERSE_LANDSCAPE){
				g.translate(0,docHeight);
				g.scale(1.0,-1.0);
			}
			
			if(scale!=null) g.scale(scale,scale);
			
			
		    canvas.setAutoSizeUpdate(true);
		    canvas.setAutoMediaUpdate(false); //we have a correct media specification, do not update
		    canvas.getConfig().setClipViewport(false);
		    canvas.getConfig().setLoadImages(true);
		    canvas.getConfig().setLoadBackgroundImages(false);
		    
		   
		    
		    Dimension viewport=new Dimension((int)(docWidth/scale),(int)(docHeight/scale));
		    
		    
		    canvas.createLayout(viewport);				    		    
		    Viewport vp=canvas.getViewport();
		    Rectangle contentSize=vp.getBounds();
		    int xcount=(int) Math.ceil(contentSize.getWidth()/viewport.width);
		    int ycount=(int) Math.ceil(contentSize.getHeight()/viewport.height);
		    int maxPageIndex=xcount*ycount;
		    if(pageIndex>=maxPageIndex) return NO_SUCH_PAGE;
		    
		    int row=pageIndex/xcount;
		    int col=pageIndex%xcount;
		    		   
		    g.translate(0-col*viewport.width,0-row*viewport.height);
		    
		    BoxRenderer renderer=new GraphicsRenderer(g);
		    
		    vp.draw(renderer);
		    
		    
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		logger.debug(String.format("PAGE_Exists replied"));
		if(pageIndex>=pageCount) pageCount=pageIndex+1;
		return Printable.PAGE_EXISTS;
	}
	
	    	

}
